/**
 * 
 */
package edu.fjnu.fundsys.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.fjnu.fundsys.dao.FundHoldingDao;
import edu.fjnu.fundsys.domain.Client;
import edu.fjnu.fundsys.domain.FundHolding;
import edu.fjnu.fundsys.utils.DBUtils;

/**
 * @author BaiXiang
 *
 */
public class FundHoldingDaoJDBCImpl implements FundHoldingDao {

	private static final String ADD_FUND_HOLDING = "insert into fund_holding values(seq_hid_id.nextval,?,?,?)";
	private static final String GET_FUND_HOLDING = "select * from fund_holding where account_no=? and fund_no=? order by hid desc";
	private static final String UPDATE_FUND_HOLDING = "update fund_holding set amount = amount + ? where hid=?";
	private static final String DEL_FUND_HOLDING = "delete from fund_holding where hid=?";
	private static final String LOAD_BY_ACCOUNT_NO = "select * from fund_holding where account_no=? order by hid desc";
	
	public void add(FundHolding fundholding) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		
		try {
			pstmt = conn.prepareStatement(ADD_FUND_HOLDING);
			pstmt.setInt(1, fundholding.getAccountNo());
			pstmt.setInt(2, fundholding.getFundNo());
			pstmt.setInt(3, fundholding.getAmount());
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, null);
		}
	}
	
	public FundHolding getFundHloding(FundHolding fundholding) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		FundHolding fundHolding = null;
		
		try{
			pstmt = conn.prepareStatement(GET_FUND_HOLDING);
			pstmt.setInt(1, fundholding.getAccountNo());
			pstmt.setInt(2, fundholding.getFundNo());
			rset = pstmt.executeQuery();

			if(rset.next()){
				fundHolding = new FundHolding();
				fundHolding.setHid(rset.getInt("hid"));
				fundHolding.setAmount(rset.getInt("account_no"));
				fundHolding.setFundNo(rset.getInt("fund_no"));
				fundHolding.setAmount(rset.getInt("amount"));
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return fundHolding;
	}
	
	public void update(FundHolding fundholding) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		
		try {
			pstmt = conn.prepareStatement(UPDATE_FUND_HOLDING);
			pstmt.setInt(1, fundholding.getAmount());
			pstmt.setInt(2, fundholding.getHid());
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, null);
		}
	}

	public void delete(Integer hid) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		
		try {
			pstmt = conn.prepareStatement(DEL_FUND_HOLDING);
			pstmt.setInt(1, hid);
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, null);
		}
	}

	public List<FundHolding> loadByAccountNo(Integer accountno) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<FundHolding> fundHoldingList = null;
		
		try{
			pstmt = conn.prepareStatement(LOAD_BY_ACCOUNT_NO);
			pstmt.setInt(1, accountno);
			rset = pstmt.executeQuery();
			
			fundHoldingList = new ArrayList<FundHolding>();
			while(rset.next()){
				FundHolding fundHolding = new FundHolding();
				fundHolding.setHid(rset.getInt("hid"));
				fundHolding.setAccountNo(rset.getInt("account_no"));
				fundHolding.setFundNo(rset.getInt("fund_no"));
				fundHolding.setAmount(rset.getInt("amount"));
				
				fundHoldingList.add(fundHolding);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return fundHoldingList;
	}

	public int cntInfos(Integer accountNo) {
		// TODO Auto-generated method stub
		String sql = "select count(*) cnt_infos from fund_holding where account_no=?";
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		int cnt = 0;
		try{
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, accountNo);
			rset = pstmt.executeQuery();

			if(rset.next()){
				cnt = rset.getInt("cnt_infos");
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return cnt;
	}

	public int cntInfos(FundHolding fundholding) {
		// TODO Auto-generated method stub
		String sql = "select count(*) cnt_infos from fund_holding where account_no=? and fund_no =?";
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		int cnt = 0;
		try{
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, fundholding.getAccountNo());
			pstmt.setInt(2, fundholding.getFundNo());
			rset = pstmt.executeQuery();

			if(rset.next()){
				cnt = rset.getInt("cnt_infos");
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return cnt;
	}

	public List<FundHolding> getSelectInfos(Integer accountNo, Integer begin,
			Integer end) {
		// TODO Auto-generated method stub
		String sql = "select * from (select rownum rn, a.* from (select * from fund_holding where account_no="+accountNo+" order by hid desc) a where rownum<=? ) where rn>=?";
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<FundHolding> fundHoldingList = null;
		
		try{
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, end);
			pstmt.setInt(2, begin);
			rset = pstmt.executeQuery();
			
			fundHoldingList = new ArrayList<FundHolding>();
			while(rset.next()){
				FundHolding fundHolding = new FundHolding();
				fundHolding.setHid(rset.getInt("hid"));
				fundHolding.setAccountNo(rset.getInt("account_no"));
				fundHolding.setFundNo(rset.getInt("fund_no"));
				fundHolding.setAmount(rset.getInt("amount"));
				
				fundHoldingList.add(fundHolding);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return fundHoldingList;
	}

	public List<FundHolding> getSelectInfos(FundHolding fundholding,
			Integer begin, Integer end) {
		// TODO Auto-generated method stub
		String subsql = "select * from fund_holding where account_no="+fundholding.getAccountNo()+" and fund_no="+fundholding.getFundNo() +" order by hid desc";
		String sql = "select * from (select rownum rn, a.* from ("+subsql+") a where rownum<=? ) where rn>=?";
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<FundHolding> fundHoldingList = null;
		
		try{
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, end);
			pstmt.setInt(2, begin);
			rset = pstmt.executeQuery();
			
			fundHoldingList = new ArrayList<FundHolding>();
			while(rset.next()){
				FundHolding fundHolding = new FundHolding();
				fundHolding.setHid(rset.getInt("hid"));
				fundHolding.setAccountNo(rset.getInt("account_no"));
				fundHolding.setFundNo(rset.getInt("fund_no"));
				fundHolding.setAmount(rset.getInt("amount"));
				
				fundHoldingList.add(fundHolding);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return fundHoldingList;
	}


}
